
var http = require('http');
var fs = require('fs');


//创建服务
var myServer = http.createServer(function (req, res, next) {
  // var html = fs.readFileSync('./view/index.html')
  var html = "哈哈"
  // res.writeHead(200, {'Content-Type': 'text/plain'}); //下载的
  res.writeHead(200, { 'Content-Type': 'text/html' }); //下载的
  // res.setHeader("Content-Type", "text/html");
  // res.setHeader("Set-Cookie", ["type=ninja", "language=javascript"]);
  // res.header("Access-Control-Allow-Origin", "*");
  // res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
  // res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  // res.header("Cache-Control", "no-store");//304
  // next();
  res.write(html);
  res.end();

}).listen('7890', function (err) {
  if (err) {
    console.log(err);
    throw err;
  }
  console.log("服务器已开启。端口号为:7890");
})

//_______________________________________________________________________________________________

var http = require('http');
var fs = require('fs');
var url = require('url');


http.createServer(function (req, res) {
  var hostname = req.headers.host;  //获取requset信息中的host地址
  var pathname = url.parse(req.url).pathname;  //获取pathname
  if (pathname === '/') {         //判断是否为域名地址（简单路由）
    readFileAndResponse('/index.html', res);
  }
}).listen(端口);


//读取文件并返回response
function readFileAndResponse(pathname, response) {
  fs.readFile(pathname.substr(1), '', function (err, data) {
    if (err) {   //文件不存在或读取错误返回404，并打印page not found
      response.writeHead(404);
      response.end('page not found');
    }
    else {
      response.end(data);   //读取成功返回相应页面信息
    }
  });
}

//_______________________________________________________________________________________________

// var https = require('https');
// var option={
//     hostname:'m.baidu.com',
//     path:'/tcx?appui=alaxs&page=api/chapterList&gid=4315647048&pageNum=1&chapter_order=asc&site=&saveContent=1',
//     headers:{
//       'Accept':'*/*',
//       'Accept-Encoding':'utf-8',  //这里设置返回的编码方式 设置其他的会是乱码
//       'Accept-Language':'zh-CN,zh;q=0.8',
//       'Connection':'keep-alive',
//       'Cookie':'BAIDUID=A78C39414751FF9349AAFB0FDA505058:FG=1; true; __bsi=12248088537049104479_00_7_N_R_33_0303_cca8_Y',
//       'Host':'m.baidu.com',
//       'Referer':'https://m.baidu.com/tcx?appui=alaxs&page=detail&gid=4305265392&from=dushu'

//     }
//   };
// https.get(option,function(res){
//   var chunks = [];
//   res.on('data',function(chunk){
//     chunks.push(chunk);
//   })
//   res.on('end',function(){
//     console.log(Buffer.concat(chunks).toString());
//   })
// })

// ——————————————————————————————————————————————————————————————————————————————————————————————
const pako = require('pako');
const http = require('http');
const url = require('url');

const server = http.createServer((req, res) => {
  req.setEncoding('utf8');

  const parsed = url.parse(req.url, true);
  const pathname = parsed.pathname;

  if (pathname === '/user') {
    let data = '';
    req.on('data', (chunk) => {
      data += chunk;
    });

    req.on('end', () => {
      const user = JSON.parse(pako.inflate(data, { to: 'string' }));
      console.log(user);

      res.writeHead(200, {
        'Content-Type': 'application/json'
      });
      res.end(pako.deflate(JSON.stringify({result: 'success'}), { to: 'string' }));
    });
  } else {
    res.writeHead(404);
    res.end(pako.deflate(JSON.stringify({result: 'error'}), { to: 'string' }));
  }
});

server.listen(3333);